package com.PublicChest.plugin.command;

import com.PublicChest.plugin.PublicChest;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import java.io.File;
import java.io.IOException;


public class PublicChestCommand implements CommandExecutor {
    private final PublicChest plugin;

    public PublicChestCommand(PublicChest plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if (command.getName().equals("pchest")) {
            if (!(commandSender instanceof Player)) {
                commandSender.sendMessage(ChatColor.RED + "[PublicChest] Only Use On Players!");
                return true;
            }

            Player sender = (Player) commandSender;
            String targetPlayer;

            if (strings.length == 0) {
                //Open self
                targetPlayer = sender.getName();
                if (this.plugin.onVisiting.get(targetPlayer) != null) {
                    sender.sendMessage(ChatColor.RED + "[PublicChest] Your Chest Is On Using!");
                    return true;
                }

                Inventory inv = Bukkit.createInventory(null, 54, targetPlayer);


                File invFile = new File(this.plugin.dataFolder + "/" + targetPlayer + ".yml");
                YamlConfiguration yml = YamlConfiguration.loadConfiguration(invFile);
                if (!invFile.exists()) {
                    //从来没开过
                    try {
                        invFile.createNewFile();
                        yml.set("Item", null);
                    } catch (IOException e) {
                        e.printStackTrace();
                        sender.sendMessage(e.getMessage());
                    }

                } else {
                    //开过了
                    inv.clear();
                    ConfigurationSection ymlSection = yml.getConfigurationSection("Item");
                    if (yml.contains("Item"))
                        for (String index : ymlSection.getKeys(false)) {
                            inv.setItem(Integer.parseInt(index), ymlSection.getItemStack(index));
                        }

                }
                this.plugin.onVisiting.put(targetPlayer, true);
                sender.openInventory(inv);
            } else {
                //Open others
                targetPlayer = strings[0];

                if (this.plugin.onVisiting.get(targetPlayer) != null) {
                    sender.sendMessage(ChatColor.RED + "[PublicChest] " + targetPlayer + "'s Chest Is On Using!");
                    return true;
                }
                try {
                    YamlConfiguration lockFileYml = YamlConfiguration.loadConfiguration(this.plugin.LockFile);
                    if (lockFileYml.contains(targetPlayer)) {
                        sender.sendMessage(ChatColor.RED + "[PublicChest] " + targetPlayer + "'s Chest Is LOCKED!");
                        return true;
                    }
                } catch (NullPointerException e) {
                    sender.performCommand("title @a title " + e.getMessage());
                }

                File invFile = new File(this.plugin.dataFolder + "/" + targetPlayer + ".yml");
                YamlConfiguration yml = YamlConfiguration.loadConfiguration(invFile);
                Inventory inv = Bukkit.createInventory(null, 54, targetPlayer);
                if (!invFile.exists()) {
                    sender.sendMessage(ChatColor.RED + "[PublicChest] " + targetPlayer + " Has NOT Registered A Public Chest Yet!");
                    return true;
                } else {
                    inv.clear();
                    ConfigurationSection ymlSection = yml.getConfigurationSection("Item");
                    if (yml.contains("Item"))
                        for (String index : ymlSection.getKeys(false)) {
                            inv.setItem(Integer.parseInt(index), ymlSection.getItemStack(index));
                        }
                }
                this.plugin.onVisiting.put(targetPlayer, true);
                sender.openInventory(inv);
            }
            return true;
        } else if (command.getName().equals("lockchest")) {
            YamlConfiguration lockFileYml = YamlConfiguration.loadConfiguration(this.plugin.LockFile);
            lockFileYml.set(commandSender.getName(), true);

            try {
                lockFileYml.save(this.plugin.LockFile);
            } catch (IOException e) {
                this.plugin.getLogger().info("PublicChest CANNOT Save File!");
                commandSender.sendMessage(ChatColor.RED + "[PublicChest] Lock Chest Failed!");
                return true;
            }
            commandSender.sendMessage(ChatColor.GREEN + "[PublicChest] Lock Chest Successfully!");
            return true;
        } else if (command.getName().equals("unlockchest")) {
            YamlConfiguration lockFileYml = YamlConfiguration.loadConfiguration(this.plugin.LockFile);
            lockFileYml.set(commandSender.getName(), null);
            try {
                lockFileYml.save(this.plugin.LockFile);
            } catch (IOException e) {
                this.plugin.getLogger().info("PublicChest CANNOT Save File!");
                commandSender.sendMessage(ChatColor.RED + "[PublicChest] Unlock Chest Failed!");
                return true;
            }
            commandSender.sendMessage(ChatColor.GREEN + "[PublicChest] Unlock Chest Successfully!");
            return true;
        }
        return true;
    }

}
